<?
//##################################################################################
class History {
	//----------------------------------------------------------------------
	var $db_handler;
	//----------------------------------------------------------------------
	function History($db_handler){
		if(!is_object($db_handler)){
			trigger_error("Not A Valid DataBase Handling Object", E_USER_ERROR);
		}else{
			$this->db_handler = $db_handler;
		}
	}
	//----------------------------------------------------------------------
	function validate_history_record($history_record,$action){
		$database = $this->db_handler->database;
		switch($action){
			case 'add':
				if($database == 'rp2'){
					$params['obligitary'] = array('reseller_id','history_table','history_type','history_data');
					$params['optional'] = array('client_id','history_date');
				}elseif($database == 'ph2'){
					$params['obligitary'] = array('client_id','history_table','history_type','history_data');
					$params['optional'] = array('history_date');
				}
			break;
			case 'select':
				if($database == 'rp2'){
					$params['obligitary'] = array('reseller_id','history_table','history_type');
					$params['optional'] = array('client_id','history_date','history_data');
				}elseif($database == 'ph2'){
					$params['obligitary'] = array('client_id','history_table','history_type');
					$params['optional'] = array('history_date','history_data');
				}
			break;
			case 'delete':
				if($database == 'rp2'){
					$params['obligitary'] = array('history_id');
				}elseif($database == 'ph2'){
					$params['obligitary'] = array('history_id');
				}
			break;
		}
		return ($this->check_history_record($history_record,$params));
	}
	//----------------------------------------------------------------------
	function check_history_record($history_record,$params){
		foreach ($params['obligitary'] as $k => $v){
			if(empty($history_record[$v]) or 
			(in_array($v,array('reseller_id','client_id','history_id')) and !is_numeric($history_record[$v])) or
			(in_array($v,array('history_table','history_type')) and !is_string($history_record[$v])) or
			($v=='history_date' and !preg_match("/\d{4}-\d{2}-\d{2}/",$history_record[$v])) or
			($v=='history_data' and !$this->parse_history_data($history_record[$v]))){
				return false;
			}
		}
		if($params['optional']){
			foreach ($params['optional'] as $k => $v){
				if((!empty($history_record[$v]) and in_array($v,array('reseller_id','client_id','history_id')) and !is_int($history_record[$v])) or
				(!empty($history_record[$v]) and in_array($v,array('history_table','history_type')) and !is_string($history_record[$v])) or
				(!empty($history_record[$v]) and $v=='history_date' and !preg_match("/\d{4}-\d{2}-\d{2}/",$history_record[$v])) or
				(!empty($history_record[$v]) and $v=='history_data' and !$this->parse_history_data($history_record[$v]))){
					return false;
				}
			}
		}
		return true;
	}
	//----------------------------------------------------------------------
	function parse_history_data($history_data){
		$result = array();
		if(! $changed_variables_array = explode('|',$history_data))
			return $result;
		foreach($changed_variables_array as $num => $change){
			if(! $tmp = explode('=>',$change)){
				return $result;
			}else{
				$res[trim($tmp[0])] = trim($tmp[1]);
			}
		}
		$result = $res;
		return $result;
	}
	//----------------------------------------------------------------------
	function manage_history($history_record,$action){
		$result = array();
		if(!in_array($action,array('add','select','delete'))){
			trigger_error("Invalid Action", E_USER_WARNING);
			return $result;
		}
		$database = $this->db_handler->database;
		if(! in_array($database,array('ph2','rp2'))){
			trigger_error("Not supported database", E_USER_WARNING);
			return $result;
		}
		if(!$this->validate_history_record($history_record,$action)){
			return $result;
		}
		switch($action){
			case 'add':
				foreach ($history_record as $k => $v){
					if(!empty($v)){
						$query_params['keys'][]=$k;
						$query_params['values'][]="'$v'";
					}
				}
				if(!$this->db_handler->query('INSERT INTO history ('.implode(',',$query_params['keys']).') '.'VALUES('.implode(',',$query_params['values']).')')){
					trigger_error($this->db_handler->error, E_USER_WARNING);
					return $result;
				}else{
					$oid = $this->db_handler->getInsertId();
					$this->db_handler->query("SELECT history_id FROM history WHERE oid=$oid");
					$tmp_history_id  = $this->db_handler->getNext();
					$history_record['history_id'] = $tmp_history_id['history_id'];
					$result = $history_record;
				}
			break;
			case 'select':
				foreach ($history_record as $k => $v){
					if(!empty($v)){
						if(in_array($k,array('history_id','reseller_id','client_id'))){
							$query_where[]="$k=$v";
						}else{
							$query_where[]="$k~*'$v'";
						}
					}
				}			
				if(!$this->db_handler->query('SELECT * FROM history WHERE '.implode(' AND ',$query_where))){
					trigger_error($this->db_handler->error, E_USER_WARNING);
					return $result;
				}else{
					$result = $this->db_handler->getAll();
				}
			break;
			case 'delete':
				if(!$this->db_handler->query('DELETE from history where history_id='.$history_record['history_id'])){
					trigger_error($this->db_handler->error, E_USER_ERROR);
					return $result;
				}else{
					$result = $history_record;
				}
			break;
		}
		return $result;
	}
	//----------------------------------------------------------------------
	function create_history_data($old_data,$new_data){
		if(is_array($old_data) and is_array($new_data) and !empty($old_data) and !empty($new_data)){
			foreach($old_data as $k=>$v){
				if($new_data[$k] and $v != $new_data[$k] and is_string($new_data[$k]) and is_string($v)){
					$result[$k] = $v;
				}
			}
		}
		if(!empty($result)){
			foreach($result as $k => $v){
				$tmp[]="$k=>$v";
			}
		}
		if($tmp){
			return implode('|',$tmp);
		}else{
			return ('');
		}
	}
}
/*
//##################################################################################
//----------------------------------------------------------------------
//validates the history_record $history_record = array('reseller_id'=>,'client_id'=>,'history_table'=>,'history_type'=>,'history_data'=>)
//reseller_id,history_table,history_type,history_data - zadalje;client_id,hostory_date ne e zadalje;returns boolean

function validate_history_record($history_record,$type='insert'){
	if(
	(!is_int($history_record['reseller_id']) or !is_string($history_record['history_table']) or !is_string($history_record['history_type']) or !is_string($history_record['history_data'])) or
	(!empty($history_record['client_id']) and !is_int($history_record['client_id'])) or
	($type='insert' ? !parse_history_data($history_record['history_data']) : false) or
	(!empty($history_record['history_date']) and !preg_match("/\d{4}-\d{2}-\d{2}/",$history_record['history_date']))
	){
		return false;
	return true;
}
//----------------------------------------------------------------------	
//parse the $history_data => reseller_id => 2323|asdsd=>213123|sdasdasd=>r43r34t54t
//returns array
function parse_history_data($history_data){
	$result = array();
	if(! $changed_variables_array = explode('|',$history_data))
		return $result;
	foreach($changed_variables_array as $num => $change){
		if(! $tmp = explode('=>',$change)){
			return $result;
		}else
			$res[trim($tmp[0])] = trim($tmp[1]);
		}
	}
	$result = $res;
	return $result;
}
//----------------------------------------------------------------------
//insert a record into history table.returns boolean
function save_to_history($history_record){
	$ret = 0;
	if(!validate_history_record($history_record))
		return $ret;
	foreach($history_record as $k => $v){
		$SQL['fields'][] = $k;
		$SQL['values'][] = '$v';
	}
	$master_db -> query('INSERT INTO history ('.implode(',', $SQL['fields']).') VALUES ('.implode(',',$SQL['values']).')');
	if(!$master_db->error){
		$master_db->free();
		return true;
	}else{
		$master_db->free();
		return false;
	}
}
//----------------------------------------------------------------------
//insert a record into history table.returns boolean
function get_history_records($history_record) {
	$ret = array();
	if(!validate_history_record($history_record,'select'))
		return $ret;
	foreach($history_record as $k => $v){
		if(!empty($v)){
			if(is_int($v)){
				$where[] = "$k=$v";
			}elseif(is_string($v)){
				$where[] = "$k~*'$v'";
			}
		}
	}
	$master_db -> query('SELECT * FROM history WHERE '.implode(' AND ',$where));
	if(!$master_db->error){
		$ret = $master_db->getAll();
		$master_db->free();
		return $ret;
	}else{
		$master_db->free();
		return $ret;	
	}
}
//##################################################################################
*/
?>
